//$(function(){
//	plusReady();
//});
function plusReady() {
    //显示消费信息
    var id = getUrlParam("id");
    var user = userLogin();


    PaintPage(id, user);

    // 绑定方法
    BindEvent(id);
}
if (window.plus) {
    plusReady();
} else {
    document.addEventListener('plusready', plusReady, false);
}

function PaintPage(id, user) {
    $.ajax({
        type: "POST",
        url: configManager.RequstUrl + "/api/order/info",
        async: true,
        data: { "userid": user.id, "token": user.token, "orderid": id }
    }).done(function (data) {
        if ("success" == data.state) {
            var order = data.data;

            var html = [];
            //订单图片
            html.push("<div class='item-02-wrap t-c'><img src='' id='orderbarcode' width='168' height='168' /><p class='mt10'>订单号码："+ order.order_sn +"&nbsp; &nbsp;&nbsp;&nbsp;订单金额:"+ order.money +"</p></div>");
            
            //订单状态
            var moneytips = "本单未付金额:" + order.money;
            var classname = 'off-org-block';
            if( ( 0 == order.payment ) && ( 1 < order.status )  ){
        		moneytips = "您已经支付金额";
        		classname = 'off-green-block';
            }
            else if( 0 < order.status ){
                moneytips = "您已经支付金额";
                classname = 'off-green-block';
            }
            
            html.push("<div class='item-02-wrap t-c'><span class='"+ classname +"'>"+ moneytips +"</span></div>");
            
            
            //订单商品
            html.push('<div class="table-list-wrap"><table><tbody><tr> <th width="50%">项目名称</th><th>数量</th><th>单价</th></tr>');
            $.each(order.goods, function(i, goods) {
            	html.push('<tr><td>'+ goods.goods_title +'</td><td>'+ goods.quantity +'</td><td>'+ goods.goods_price +'</td></tr>')
            });
            html.push('</tbody></table></div>');

            $("#content").html(html.join(""));

            $.ajax({
                type: 'Get',
                url: configManager.RequstUrl + 'api/common/qrcode?type=order&order=' + order.id
            }).done(function (data) {
                if (data.state != "success") {
                    console.log(data.message);
                    return;
                }
                $("#orderbarcode").attr("src", data.uri);
            });


        }
        else {
            plus.ui.alert(data.message, function () { }, configManager.alerttip, configManager.alertCtip);
        }
    });

}

function BindEvent() {
	
	//清除临时订单和购物车中的商品
	clearCachedCart();	
	
    // 保存到相册
    $("#content").on("click", "#savepic", function () {
        saveGallery($("#orderbarcode").attr("src"));
    });

    // 回退
    $("#back").on("click", function () {
        var mainview = plus.webview.getWebviewById(pageName.main);
        mainview.evalJS("gohome()");
    });

}

function saveGallery(url) {
    var dtask = plus.downloader.createDownload(url, {}, function (d, status) {
        // 下载完成
        if (status == 200) {
            console.log(d.filename);
            var filepath = plus.io.convertLocalFileSystemURL(d.filename);
            console.log(filepath);
            // 保存相册
            plus.gallery.save(filepath);
            plus.nativeUI.alert("保存成功");
            // 删除
            plus.io.resolveLocalFileSystemURL(filepath, function (entry) {
                entry.remove(function (e) { }, function (e) { });
            }, function (e) { });

        } else {
            plus.nativeUI.alert("Download failed: " + status);
        }
    });
    //dtask.addEventListener( "statechanged", onStateChanged, false );
    dtask.start();
}

//删除购物车中的当前商品，临时订单
function clearCachedCart(){
	// 清空购物车当前商品
	var mycart = JSON.parse(plus.storage.getItem(storageManager.cart));
	var tempmycart = [];
	// 如果不是本商店商品，清空购物车
//	console.log(JSON.stringify(mycart));
	
	for (var i = 0; (mycart>0) && (i < mycart.length); i++) {
	    var goods = mycart[i];
	    if (goods && !goods.selected) {
	        tempmycart.push(goods);
	    }
	}
//	console.log(JSON.stringify(tempmycart));
	
	plus.storage.setItem(storageManager.cart, JSON.stringify(tempmycart));					
	//清除临时订单
	plus.storage.removeItem(storageManager.temporder);

}